home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 12 - 1996 / 12.10 Oct 96 / EvansJavaNet / JavalogServer.java < prev    next >
Encoding:
Java Source  |  1996-06-14  |  1.9 KB  |  71 lines  |  [TEXT/Rstr]

  1. /*
  2.     JavalogServer.java
  3.     
  4.     A very basic implemtation of syslog in Java using UDP datagram
  5.     sockets
  6.     
  7.     By Christopher Evans
  8.     Copyright © 1996 Natural Intelligence, Inc.
  9. */
  10. import java.net.*;
  11. import java.io.*;
  12.  
  13. /**
  14. Implements a basic syslog server receiving data on port 514 and
  15. logging it to a file called syslog.log
  16. */ 
  17.  
  18. public class JavalogServer
  19. {
  20.     
  21.     public static void main(String args[]) throws Exception
  22.     {
  23.         //Create the buffer to store the data as it comes in
  24.         byte[]        log_buffer = new byte[2048];
  25.         
  26.         int            received_messages = 0;
  27.         
  28.         //open the file for writing the log messages
  29.         File        out_file = new File("syslog.log");
  30.         
  31.         //create the output stream so we can dump the data
  32.         FileOutputStream syslog_file = new FileOutputStream(out_file);
  33.         
  34.         //Create a DatagramPacket to receive the incoming log data
  35.         DatagramPacket    packet = new DatagramPacket(log_buffer, log_buffer.length);
  36.         
  37.         //Create a socket that listens on the net
  38.         DatagramSocket    socket = new DatagramSocket(514);
  39.         
  40.         while(received_messages < 5) 
  41.         {
  42.             //Wait until some data arrives.  Aren't threads great?
  43.             socket.receive(packet);
  44.             
  45.             //Increment the message count
  46.             received_messages++;
  47.             
  48.             //Build a string of the packet data
  49.             String    packet_string = new String(log_buffer, 0, 0, packet.getLength());
  50.             
  51.             //Put the packet data after a bit of header so we can see where it comes from
  52.             String     out_string = "<syslog from "+packet.getAddress().getHostName()+">" + packet_string + "\n";
  53.             
  54.             //print the message to the standard out window
  55.             System.out.println(out_string);
  56.             
  57.             //convert the message to an array of bytes so it can be sent to the file
  58.             int msg_len = out_string.length();
  59.             
  60.             byte[]    out_buffer = new byte[msg_len];
  61.             
  62.             out_string.getBytes(0, out_string.length(), out_buffer, 0);
  63.  
  64.             //write the name of the host where the data came from to the file
  65.             syslog_file.write(out_buffer, 0, out_string.length());
  66.             
  67.         }
  68.         socket.close();
  69.     }    
  70. }
  71.